package com.dao.impl;

import com.dao.IFlightDao;
import com.model.FlightModel;
import com.utils.JdbcUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Program: exam
 * @ClassName: FlightDaoImpl
 * @Description:
 * @Author: li
 * @Create: 2024-12-23 13:13
 */
public class FlightDaoImpl implements IFlightDao {
    JdbcTemplate jt = new JdbcTemplate(JdbcUtils.getDs());

    //返回所有的航班信息
    @Override
    public List<FlightModel> queryAllFlight() {
        String sql = "select * from  flightport  order by fname";
        return jt.query(sql, new BeanPropertyRowMapper<FlightModel>(FlightModel.class));
    }

    //按租金倒序
    @Override
    public List<FlightModel> queryAllFlightByRentDesc() {
        String sql = "select * from  flightport where status = 1 order by crent desc";
        return jt.query(sql, new BeanPropertyRowMapper<FlightModel>(FlightModel.class));
    }

    //按租金升序
    @Override
    public List<FlightModel> queryAllFlightByRentAsc() {
        String sql = "select * from flightport where status = 1  order by crent";
        return jt.query(sql, new BeanPropertyRowMapper<FlightModel>(FlightModel.class));
    }

    //根据航班模糊查询
    @Override
    public List<FlightModel> fuzzyQueryByFlight(String name) {
        if(StringUtils.isNotBlank(name)){
            return jt.query(name, new BeanPropertyRowMapper<FlightModel>(FlightModel.class));

        }else {
            return jt.query("select * from flightport where 1=1",new BeanPropertyRowMapper<FlightModel>(FlightModel.class));
        }

    }

    //根据航班id 返回 Flight
    @Override
    public FlightModel queryFlightById(int id) {
        String sql = "select * from flightport where fid = ?";
        return jt.query(sql, new BeanPropertyRowMapper<FlightModel>(FlightModel.class), id).get(0);
    }

    //更改航班状态
    @Override
    public void changeFlightStatus(int id, int status) {
        String sql = "update flightport set fstatus = ? where fid = ?";
        jt.update(sql, status, id);
    }

    //分页加模糊查询,返回Flight
    @Override
    public List<FlightModel> pageFlight(int pageStart, int pageSize, Map<String, String[]> map) {
        StringBuilder sql = new StringBuilder();
        List<Object> list = new ArrayList<>();
        sql.append("select * from flightport where fstatus != 0");

        Set<String> sets = map.keySet();

        for (String set : sets) {
            if ("".equals(map.get(set)[0]) || "".equals(map.get(set)[0])) {
                continue;
            } else if ("currentPage".equals(set) || "pageSize".equals(set)) {
                continue;
            } else if ("status".equals(set)) {
                sql.append(" and " + set + " like ?");
                list.add(map.get(set)[0]);
            } else {
                sql.append(" and " + set + " like ?");
                list.add("%" + map.get(set)[0] + "%");
            }
        }

        sql.append(" limit ?,?");
        list.add(pageStart);
        list.add(pageSize);

        return jt.query(sql.toString(), new BeanPropertyRowMapper<FlightModel>(FlightModel.class), list.toArray());
    }

    //返回模糊查询的数据总数
    @Override
    public int totalData(Map<String, String[]> map) {
        StringBuilder sql = new StringBuilder();
        List<Object> list = new ArrayList<>();
        sql.append("select count(1) from flightport where status != 0");

        Set<String> sets = map.keySet();

        for (String set : sets) {
            if ("".equals(map.get(set)[0]) || "".equals(map.get(set)[0])) {
                continue;
            } else if ("currentPage".equals(set) || "pageSize".equals(set)) {
                continue;
            } else if ("status".equals(set)) {
                sql.append(" and " + set + " like ?");
                list.add(map.get(set)[0]);
            } else {
                sql.append(" and " + set + " like ?");
                list.add("%" + map.get(set)[0] + "%");
            }
        }

        return jt.queryForObject(sql.toString(), Integer.class, list.toArray());
    }

    //下架
    @Override
    public void changFlightDown(int fid) {
        String sql = "update flightport set fstatus = -1 where fid = ?";
        jt.update(sql, fid);
    }

    //添加航线
    @Override
    public void addFlight(FlightModel flightModel) {
        String sql = "insert into flightport values(null,?,?,?,?,?,?,?,?,?)";
        jt.update(sql, flightModel.getFfrom(), flightModel.getFto(), flightModel.getFimg(), flightModel.getFinfo(), flightModel.getFname(), flightModel.getFrent(), flightModel.getfStatus(), flightModel.getFstart(), flightModel.getFend() );
    }
    @Override
    public void updateFlight(FlightModel flightModel) {
        String sql = "update flightport set fimg = ?, finfo = ?, fname = ?, frent = ?, fstatus = ?,ffrom=?,fto=?,fstart=?,fend=? where fid = ?";
        jt.update(sql, flightModel.getFimg(), flightModel.getFinfo(), flightModel.getFname(), flightModel.getFrent(), flightModel.getfStatus(), flightModel.getFfrom(), flightModel.getFto(), flightModel.getFstart(), flightModel.getFend(),flightModel.getFid());
    }

    @Override
    public int totalFlight() {
        String sql = "select count(1) from flightport";
        return jt.queryForObject(sql, Integer.class);
    }


}
